<template>
    <div class="user-stats-card">
        <div class="user-header">
            <img :src="userStore.userInfo.avatar || '/default-avatar.png'" alt="头像" class="current-user-avatar" />
            <div class="user-basic">
                <h3 class="current-username">{{ userStore.getDisplayName() }}</h3>
                <p class="user-email">{{ userStore.getUserEmail() }}</p>
            </div>
        </div>

        <div class="stats-section">
            <div class="stat-item" @click="showFollowing">
                <div class="stat-number">{{ stats.following }}</div>
                <div class="stat-label">关注</div>
            </div>
            <div class="stat-item" @click="showFollowers">
                <div class="stat-number">{{ stats.followers }}</div>
                <div class="stat-label">粉丝</div>
            </div>
        </div>

        <div class="actions-section">
            <el-button type="primary" @click="goToProfile" class="profile-btn">
                编辑资料
            </el-button>
        </div>
    </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { useFollowsStore } from '@/stores/follow'

const router = useRouter()
const userStore = useUserStore()
const followsStore = useFollowsStore()

const stats = ref({
    following: 0,
    followers: 0
})

// 加载统计数据
const loadStats = async () => {
    try {
        const userId = userStore.getUserId()

        // 获取关注列表统计
        const followingResult = await followsStore.fetchFollowingList(userId, { page: 1, pageSize: 1 })
        if (followingResult.code === 0) {
            stats.value.following = followingResult.data.pagination?.total || 0
        }

        // 获取粉丝列表统计
        const followersResult = await followsStore.fetchFollowersList(userId, { page: 1, pageSize: 1 })
        if (followersResult.code === 0) {
            stats.value.followers = followersResult.data.pagination?.total || 0
        }
    } catch (error) {
        console.error('加载统计数据失败:', error)
    }
}

// 监听关注状态变化事件
const handleFollowStatusChange = () => {
    loadStats()
}

// 显示关注列表
const showFollowing = () => {
    const userId = userStore.getUserId()
    router.push(`/follow-list/${userId}`)
}

// 显示粉丝列表
const showFollowers = () => {
    const userId = userStore.getUserId()
    router.push(`/follow-list/${userId}`)
}

// 跳转到个人资料页
const goToProfile = () => {
    router.push('/profile')
}

onMounted(() => {
    loadStats()
    // 添加事件监听
    window.addEventListener('followStatusChanged', handleFollowStatusChange)
})

onUnmounted(() => {
    // 移除事件监听
    window.removeEventListener('followStatusChanged', handleFollowStatusChange)
})
</script>
<style scoped>
.user-stats-card {
    background: white;
    border-radius: 12px;
    padding: 20px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    text-align: center;
}

.user-header {
    display: flex;
    align-items: center;
    margin-bottom: 20px;
    text-align: left;
}

.current-user-avatar {
    width: 60px;
    height: 60px;
    border-radius: 50%;
    object-fit: cover;
    margin-right: 12px;
    border: 2px solid #f0f0f0;
}

.user-basic {
    flex: 1;
}

.current-username {
    margin: 0 0 4px 0;
    font-size: 18px;
    font-weight: 600;
    color: #333;
}

.user-email {
    margin: 0;
    font-size: 14px;
    color: #666;
}

.stats-section {
    display: flex;
    justify-content: space-around;
    margin: 20px 0;
    padding: 20px 0;
    border-top: 1px solid #f0f0f0;
    border-bottom: 1px solid #f0f0f0;
}

.stat-item {
    cursor: pointer;
    transition: all 0.3s ease;
    padding: 8px 16px;
    border-radius: 8px;
}

.stat-item:hover {
    background: #f5f7fa;
    transform: translateY(-2px);
}

.stat-number {
    font-size: 24px;
    font-weight: bold;
    color: #1890ff;
    margin-bottom: 4px;
}

.stat-label {
    font-size: 14px;
    color: #666;
}

.actions-section {
    margin-top: 16px;
}

.profile-btn {
    width: 100%;
}
</style>